package com.cms.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.cms.entity.Record;
import com.cms.service.RecordService;
import com.cms.utils.StrUtil;
import com.cms.utils.page.Pagination;

@Controller
@RequestMapping(value = "/record")
public class RecordController {
    @Autowired
    private RecordService RecordService;

    @ResponseBody
    @RequestMapping(value = "/list")
    public String getRecordList(@RequestParam(defaultValue = "0") int curr, @RequestParam(defaultValue = "20") int nums,
        @RequestParam(defaultValue = "") String searchKey) {
        System.out.println("RecordController");
        Pagination<Record> page = new Pagination<Record>();

        page.setTotalItemsCount(RecordService.getTotalItemsCount(searchKey));
        page.setPageSize(nums);
        page.setPageNum(curr);

        List<Record> list = RecordService.getRecordList(page, searchKey);

        String jsonStr = StrUtil.RETURN_JONS_PRE_STR + page.getTotalItemsCount() + StrUtil.RETURN_JONS_MID_STR
            + JSON.toJSONString(list) + StrUtil.RETURN_JONS_END_STR;
        return jsonStr;
    }

    @RequestMapping(value = "/addPage")
    public ModelAndView toAddPage() {
        return new ModelAndView("/recordAdd");
    }

    /**
     * 增加，或者修改Record
     * 
     * @param opType
     * @param Record
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add")
    public String addRecord(@RequestParam(defaultValue = "2") int opType, Record Record) {
        System.out.println(opType + ":" + Record);
        int res = 0;
        if (opType == 0) {
            try {
                res = RecordService.addRecord(Record);
            } catch (Exception e) {
                System.out.println("添加失败！重复！");
                return "添加失败！重复！";
            }
            if (res > 0)
                return StrUtil.RESULT_TRUE;
            return "添加失败";
        } else if (opType == 1) {
            res = RecordService.updateRecord(Record);
            if (res > 0)
                return StrUtil.RESULT_TRUE;
            return "修改失败！";
        }
        return "error";
    }

    @ResponseBody
    @RequestMapping(value = "/delete")
    public String deleteStudnet(Integer id) {
        if (RecordService.deleteByPrimaryKey(id) > 0)
            return StrUtil.RESULT_TRUE;
        return "删除失败！";
    }

    /**
     * 批量删除
     * 
     * @param stuIds
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteList")
    public String deleteStudnetList(String stuIds) {
        System.out.println("stuIds" + stuIds);
        List<String> list = new ArrayList<String>();
        try {
            String[] ids = stuIds.split(",");
            for (String id : ids) {
                list.add(id);
            }
            if (RecordService.deleteRecord(list) > 0) {
                return StrUtil.RESULT_TRUE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败！参数出错！";//
        }
        return "删除失败！";
    }

    @ResponseBody
    @RequestMapping("/import")
    public String impotr(HttpServletRequest request, MultipartFile file) {
        // 获取上传的文件
        InputStream in = null;
        try {
            in = file.getInputStream();
            // 数据导入
            int res = RecordService.importExcelInfo(in, file);
            if (res > 0) {
                return StrUtil.RETURN_JONS_PRE_STR + "0" + StrUtil.RETURN_JONS_MID_STR + "true"
                    + StrUtil.RETURN_JONS_END_STR;
            } else {
                return StrUtil.RETURN_JONS_PRE_STR + "0" + StrUtil.RETURN_JONS_MID_STR + "false"
                    + StrUtil.RETURN_JONS_END_STR;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return StrUtil.RETURN_JONS_PRE_STR + "0" + StrUtil.RETURN_JONS_MID_STR + "error"
                + StrUtil.RETURN_JONS_END_STR;
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}
